<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <div style="padding: 10px">
        <el-button type="primary" size="mini" @click="addPermissionClick('add',0)">添加权限</el-button>
      </div>
      <!-- 展示权限表格 -->
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <!-- 如果是一级权限才会显示<添加>按钮 -->
            <el-button
              v-if="row.type === 1"
              type="text"
              size="mini"
              @click="addPermissionClick('add',row.id)"
            >添加</el-button>
            <el-button type="text" size="mini" @click="addPermissionClick('edit',row.id)">编辑</el-button>
            <el-popconfirm
              title="你确定要删除吗？"
              @onConfirm="delConfirmClick(row.id)"
            >
              <el-button slot="reference" type="text" size="mini" style="width: 50px">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加编辑权限的弹层 -->
    <el-dialog
      :visible.sync="showDialog"
      :title="showTitle"
      width="500px"
    >
      <el-form
        ref="permissionForm"
        label-width="120px"
        :model="permissionForm"
        :rules="rules"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" size="mini" style="width:300px" type="textarea" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="permissionForm.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="confirmClick">确定</el-button>
              <el-button size="mini" @click="cancelClick">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionsAPI, addPermissionAPI, getPermissionAPI, updatePermissionAPI, delPermissionAPI } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      // 当前操作类型
      actionType: '',
      // 权限列表数据
      permissionList: [],
      // 是否展示弹层
      showDialog: false,
      // 权限表单数据对象
      permissionForm: {
        name: '',
        code: '',
        description: '',
        enVisible: 1,
        type: 1,
        pid: 0
      },
      // 数据校验
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '标识不能为空', trigger: 'blur' }]
      }
    }
  },
  // 动态展示标题
  computed: {
    showTitle() {
      return this.actionType === 'add' ? '添加权限' : '编辑权限'
    }
  },
  created() {
    // 获取权限列表数据
    this.getPermissionList()
  },
  methods: {
    // 添加权限
    async addPermissionClick(type, id) {
      // 记录当前的id
      this.currentId = id
      this.showDialog = true
      this.actionType = type

      // 如果是编辑权限，就回显示到表单
      if (type === 'edit') {
        this.permissionForm = await getPermissionAPI(id)
      }
    },
    // 获取权限列表数据
    async getPermissionList() {
      // 调用权限列表接口方法: 转成树形结构搭配row-key实现树形结构展示
      this.permissionList = transListToTreeData(await getPermissionsAPI(), 0)
    },
    // 取消 重置表单 + 关闭对话框
    cancelClick() {
      this.permissionForm = {
        name: '',
        code: '',
        description: '',
        enVisible: 1,
        type: 1,
        pid: 0
      }
      this.$refs.permissionForm.resetFields()
      this.showDialog = false
    },
    // 点击确定按钮
    confirmClick() {
      this.$refs.permissionForm.validate(async(isOK) => {
        if (isOK) {
          if (this.actionType === 'add') {
            if (this.currentId) {
              this.permissionForm = { ...this.permissionForm, pid: this.currentId, type: 2 }
            }
            await addPermissionAPI(this.permissionForm)
            // 提示用户
            this.$message.success('添加权限成功')
          } else {
            await updatePermissionAPI({ ...this.permissionForm, id: this.currentId })
            this.$message.success('修改权限成功')
          }

          // 刷新权限列表
          this.getPermissionList()
          // 清空表单 关闭对话框
          this.cancelClick()
        }
      })
    },
    // 删除
    async delConfirmClick(id) {
      await delPermissionAPI(id)
      this.$message.success('删除权限成功')
      // 刷新权限列表
      this.getPermissionList()
    }

  }
}
</script>
